package com.example.weather_app;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weather_app.db.DBManager;

public class MoreActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView moreIv;
    private TextView exchangebgTv, versionTv, cacheTv, shareTv;
    private RadioGroup moreRg;
    private String msg,versionName;
    private SharedPreferences bg_pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);

        init();
    }

    private String getVersion() {
        String versionName = null;
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public void init() {
        moreIv = findViewById(R.id.more_iv);
        exchangebgTv = findViewById(R.id.more_tv_exchangebg);
        versionTv = findViewById(R.id.more_tv_version);
        cacheTv = findViewById(R.id.more_tv_cache);
        shareTv = findViewById(R.id.more_tv_share);
        moreRg = findViewById(R.id.more_rg);
        //获取版本
        versionName = getVersion();
        versionTv.setText("当前版本：    v"+versionName);
        //首选项
        bg_pref = getSharedPreferences("bg_pref", MODE_PRIVATE);

        msg = "说天气111";

        moreIv.setOnClickListener(this);
        exchangebgTv.setOnClickListener(this);
        cacheTv.setOnClickListener(this);
        shareTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_iv:
                finish();
                break;
            case R.id.more_tv_exchangebg:
                //修改背景图
                if (moreRg.getVisibility()== View.GONE) {
                    moreRg.setVisibility(View.VISIBLE);
                    setRGListener();
                }else {
                    moreRg.setVisibility(View.GONE);
                }
                break;
            case R.id.more_tv_cache:
                clearCache();
                break;
            case R.id.more_tv_share:
                shareSoftwareMsg(msg);
                break;
        }
    }

    private void setRGListener() {
        moreRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int bg = bg_pref.getInt("bg", 0);
                Intent intent = new Intent(MoreActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                switch (i) {
                    case R.id.more_rb_green:
                        if (bg == 0){
                            Toast.makeText(MoreActivity.this, "已设置当前背景", Toast.LENGTH_SHORT).show();
                        }else {
                            bg_pref.edit().putInt("bg",0).commit();
                        }
                        break;
                    case R.id.more_rb_pink:
                        if (bg == 1){
                            Toast.makeText(MoreActivity.this, "已设置当前背景", Toast.LENGTH_SHORT).show();
                        }else {
                            bg_pref.edit().putInt("bg",1).commit();
                        }
                        break;
                    case R.id.more_rb_bule:
                        if (bg == 2){
                            Toast.makeText(MoreActivity.this, "已设置当前背景", Toast.LENGTH_SHORT).show();
                        }else {
                            bg_pref.edit().putInt("bg",2).commit();
                        }
                        break;
                }
                startActivity(intent);
            }
        });
    }

    /**
     * 分享软件
     */
    private void shareSoftwareMsg(String s) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,s);
        startActivity(Intent.createChooser(intent,"说天气"));
    }

    /**
     * 清除缓存
     */
    private void clearCache() {
        //提示框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("是否删除所有缓存？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        DBManager.deleteAllInfo();
                        Toast.makeText(MoreActivity.this, "已删除所有缓存", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MoreActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("取消",null);
        builder.create().show();
    }
}